package com.yiweikeji.project.web;
import com.yiweikeji.project.core.Result;
import com.yiweikeji.project.core.ResultGenerator;
import com.yiweikeji.project.model.CustomerProtocol;
import com.yiweikeji.project.service.CustomerProtocolService;

import com.yiweikeji.project.core.ResultCode;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yiweikeji.project.core.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.annotation.Resource;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
* Created by CodeGenerator on 2019/10/16.
*/
@Api(tags = "customerProtocol管理")
@RestController
@RequestMapping("/customer/protocol")
public class CustomerProtocolController {

	private static Logger log = LoggerFactory.getLogger(CustomerProtocolController.class);

    @Resource
    private CustomerProtocolService customerProtocolService;

    @PostMapping("/add")
	@ApiOperation(value = "customerProtocol新增",httpMethod = "POST")
    public Result add(@RequestBody CustomerProtocol customerProtocol,String userId) {
    	if(customerProtocol == null){
    		return ResultGenerator.genFailResult(ResultCode.OBJECT_IS_NULL);
    	}
    	if(StringUtils.isBlank(userId)){
    		return ResultGenerator.genFailResult(ResultCode.USERID_IS_NULL);
    	}
    	try {
    //		customerProtocol.setCreateTime(new Date());
    //		customerProtocol.setCreateUserId(userId);
    		customerProtocolService.save(customerProtocol);
		} catch (Exception e) {
			log.error("新增对象操作异常e:{}",e);
			return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
		}
        
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
	@ApiOperation(value = "customerProtocol删除",httpMethod = "POST")
    public Result delete(@RequestParam Integer id,String userId) {
    	if(id == null){
    		return ResultGenerator.genFailResult(ResultCode.ID_IS_NULL);
    	}
    	if(StringUtils.isBlank(userId)){
    		return ResultGenerator.genFailResult(ResultCode.USERID_IS_NULL);
    	}
    	try {
    		customerProtocolService.deleteById(id);
		} catch (Exception e) {
			log.error("删除对象操作异常e:{}",e);
			return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
		}
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
	@ApiOperation(value = "customerProtocol更新",httpMethod = "POST")
    public Result update(@RequestBody CustomerProtocol customerProtocol,String userId) {
    	if(customerProtocol == null){
    		return ResultGenerator.genFailResult(ResultCode.OBJECT_IS_NULL);
    	}
    	if(customerProtocol.getId() == null){
    		return ResultGenerator.genFailResult(ResultCode.ID_IS_NULL);
    	}
    	if(StringUtils.isBlank(userId)){
    		return ResultGenerator.genFailResult(ResultCode.USERID_IS_NULL);
    	}
    	try {
    //		customerProtocol.setUpdateTime(new Date());
    //		customerProtocol.setUpdateUserId(userId);
    		customerProtocolService.update(customerProtocol);
		} catch (Exception e) {
			log.error("更新对象操作异常e:{}",e);
			return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
		}
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
	@ApiOperation(value = "customerProtocol获取详情",httpMethod = "POST")
    public Result<CustomerProtocol> detail(@RequestParam Integer id,String userId) {
    	if(id == null){
    		return ResultGenerator.genFailResult(ResultCode.ID_IS_NULL);
    	}
    	if(StringUtils.isBlank(userId)){
    		return ResultGenerator.genFailResult(ResultCode.USERID_IS_NULL);
    	}
    	CustomerProtocol customerProtocol = null;
    	try {
    		customerProtocol = customerProtocolService.findById(id);
		} catch (Exception e) {
			log.error("查询对象操作异常e:{}",e);
			return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
		}
        
        return ResultGenerator.genSuccessResult(customerProtocol);
    }

    @PostMapping("/list")
	@ApiOperation(value = "customerProtocol获取列表",httpMethod = "POST")
    public Result<List<CustomerProtocol>> list(@RequestBody CustomerProtocol customerProtocol, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,String userId) {
        if(StringUtils.isBlank(userId)){
    		return ResultGenerator.genFailResult(ResultCode.USERID_IS_NULL);
    	}
        PageHelper.startPage(page, size);
        
        Condition condition = new Condition(customerProtocol.getClass());
        Criteria criteria = condition.createCriteria();
//        criteria.andEqualTo("name", city.getName());
		PageInfo pageInfo = null;
		try {
    		 List<CustomerProtocol> list = customerProtocolService.findByCondition(condition);
    		 pageInfo = new PageInfo(list);
		} catch (Exception e) {
			log.error("查询对象操作异常e:{}",e);
			return ResultGenerator.genFailResult(ResultCode.INTERNAL_SERVER_ERROR);
		}
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
